/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>CreateTime: 2006-3-27 </p>
 * @author pengz
 * @version 1.0
 */

package cn.agiledata.bank.tel.service;

import cn.agiledata.bank.tel.bean.TelCallBean;
import cn.agiledata.bank.tel.util.VelocityUtil;
import cn.agiledata.bank.common.dao.AccountTemplate;
import cn.agiledata.bank.common.util.Constant;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class TelCallManagerImpl implements ITelCallManager {
	private AccountTemplate accountTemplate; //账户template
	private ITelCallService telCallService;
	
	/**
	 * 1:成功,<0:失败
	 * @return
	 */
	public int telCall(TelCallBean telBean) {
		String telNum = telBean.getTelNum();
		if(isRightTel(telNum)) {
			telBean.setTelNum(constructTel(telNum));
			telCallService.putTelData(telBean);
			return 1;
		} else {
			return 0;
		}
	}
	
	/**
	 * 个人设置里面是否可以打电话 
	 * @return
	 */
	public int ifCanCall(String subAccountId, String moduleType) {
		return accountTemplate.ifCanCall(subAccountId,moduleType);
	}
	
	/**
	 * 判断电话号码是否正确
	 * @return
	 */
	public boolean isRightTel(String tel) {
		if(tel == null) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * 构造外拨电话
	 * @param tel
	 * @return
	 */
	public static String constructTel(String tel) {
		String constructTel = tel;
		//内线
		if(constructTel.length() == 3) {
			return constructTel;
		//外线
		} else {
			if(constructTel.indexOf("-") != -1) {
				constructTel = Constant.OUT_CALL_PRE + constructTel.replaceAll("-","");
			} else if(constructTel.indexOf("*") != -1) {
				constructTel = Constant.OUT_CALL_PRE + constructTel.replaceAll("*","");
			} else {
				constructTel = Constant.OUT_CALL_PRE + constructTel;
			}
			return constructTel;
		}
	}
	
	/**
	 * 得到电话数据,封装成";"隔开的字符串
	 * @return
	 */
	public String getTelData() {
		StringBuffer sb = new StringBuffer();
		try {
			TelCallBean telBean = null;
			if (!telCallService.empty())
				telBean = (TelCallBean)telCallService.getTelData();
			if(telBean != null) {
				sb.append(telBean.getTelNum()+";");
				String data = new VelocityUtil().getVelocityString(telBean);
				sb.append(data);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return sb.toString();
		}
	}

	public void setTelCallService(ITelCallService telCallService) {
		this.telCallService = telCallService;
	}

	public ITelCallService getTelCallService() {
		return telCallService;
	}

	public AccountTemplate getAccountTemplate() {
		return accountTemplate;
	}

	public void setAccountTemplate(AccountTemplate accountTemplate) {
		this.accountTemplate = accountTemplate;
	}

}
